<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Organisation extends CI_Controller {
	
	function view($id){
		$this->load->model("organisation_model",'organisation',TRUE);
		$data["organisation"]=$this->organisation->getById($id);
		$this->load->view('detail/organisation/organisation_info_view',$data,false);
	}
	
	function loadTravel($id){
		$this->load->model("organisation_model",'organisation',TRUE);
		$this->load->model("cities_model",'cities',TRUE);
		$this->load->model("travel_model",'travel',TRUE);
		$data["organisation"]=$this->organisation->getById($id);
		$data["travels"]=$this->travel->getByParam(array("organisationFk"=>$id));
		$data["cities"]=$this->cities->getAll();
		foreach ($data["cities"] as $city){
				
			$city->fromSelected="";
			$city->toSelected="";
		
		}
		foreach ($data["cities"] as $city){
			foreach ($data["travels"] as $travel){
				$from=$travel->zipFrom;
				$to=$travel->zipTo;
				if($city->zip==$from){
					$city->fromSelected=$from;
				}
				if($city->zip==$to){
					$city->toSelected=$to;
				}
		
			}
				
				
		}
		$this->render($data,'detail/organisation/organisation_simple_edit_view','detail/travel/travel_edit_view');
		
		
	}
	
	
	function edit(){
		$id=$this->input->post("orgId");
		$button=$this->input->post("button");
		if("Editer l'organisation"==$button){
			$this->loadTravel($id);
		}elseif ("Supprimer une organisation"==$button){
			$this->delete($id);			
		}
		
	}
	
	function backendPage(){
		$this->load->model("organisation_model",'organisation',TRUE);
		$this->load->model("cities_model",'cities',TRUE);
		$this->load->model("travel_model",'travel',TRUE);
		$data["organisations"]=$this->organisation->getAll();
		$data["cities"]=$this->cities->getAll();
		$this->render($data,'detail/organisation/organisation_edit_view');
	}
	function add(){
		$this->load->model("organisation_model",'organisation',TRUE);
		$data["organisation"]=new Organisation_model();
		$this->render($data,'detail/organisation/organisation_simple_edit_view');
		
	}
	function update(){
		$id=$this->input->post("orgId");
		$this->load->model("organisation_model",'organisation',TRUE);
		$organisation=new Organisation_model();
		if($id!=""){
			$organisation->id=$id;
		}
		
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		$this->form_validation->set_rules('name', 'Nom', 'trim|required');
		$organisation->name=$this->input->post("name");
		$organisation->contactName=$this->input->post("contactName");
		$organisation->adress=$this->input->post("adress");
		$organisation->phone=$this->input->post("phone");
		$organisation->cellPhone=$this->input->post("cellPhone");
		$organisation->workHours=$this->input->post("workHours");
		$organisation->priceDesc=$this->input->post("priceDesc");
		$organisation->mutuDesc=$this->input->post("mutuDesc");
		$organisation->requestDelay=$this->input->post("requestDelay");
		$organisation->telPermanence=$this->input->post("telPermanence");
		$organisation->transportType=$this->input->post("transportType");
		$organisation->email=$this->input->post("email");
		
		$webSite=$this->input->post("webSite");
		if(""!=$webSite && strncmp($webSite, "http://", strlen("http://"))){
			$webSite="http://".$webSite;
		}
		$organisation->webSite=$webSite;
		
		if ($this->form_validation->run() == FALSE)		{
			$data["destination"]="FROM";
			$data["selectedCommune"]="none";
			$data["toCommune"]="none";
			$data["fromCommune"]="none";
				
			$this->render($data,'map/province');
		}else{
			$id=$organisation->saveOrUpdate();
			$data["organisation"]=$this->organisation->getById($id);
			$this->loadTravel($data["organisation"]->id);
		}
		
		
		
	}
	
	function addTravel(){
		$id=$this->input->post("orgId");

		$this->load->library('form_validation');
		$citiesFrom=$this->input->post("citiesFrom");
		$citiesTo=$this->input->post("citiesTo");
		
		$this->load->model("organisation_model",'organisation',TRUE);
		$this->load->model("cities_model",'cities',TRUE);
		$this->load->model("travel_model",'travel',TRUE);
		
		$data["organisation"]=$this->organisation->getById($id);
		$travel=$this->travel->getByParam(array("organisationFk"=>$id));
		foreach ($travel as $toDelete){
			$this->travel->delete($toDelete->id);
		}
		
		if(is_array($citiesFrom) && is_array($citiesTo)){
			
		
			foreach ($citiesFrom as $cityFrom){
				
				foreach ($citiesTo as $cityTo){
					
						$travel=new Travel_model();
						$travel->organisationFk=$id;
						$travel->zipFrom=$cityFrom;
						$travel->zipTo=$cityTo;
						$travel->saveOrUpdate();
					
				}	
			}
		}
		$this->loadTravel($id);
	}
	
	
	function delete($id){
		$this->load->model("organisation_model",'organisation',TRUE);
		$this->load->model("travel_model",'travel',TRUE);
		
		$travel=$this->travel->getByParam(array("organisationFk"=>$id));
		foreach ($travel as $toDelete){
			$this->travel->delete($toDelete->id);
		}
		$this->organisation->delete($id);
		
		$this->backendPage();
	}
	
	
	
}